package aad.storyboard.model;

import java.util.HashMap;

import aad.storyboard.controller.FadeStrategyProvider;
import android.graphics.Bitmap;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

/**
 * Constitutes the Context for the Fader strategies -> requires a concrete Fader in the constructor
 
 */
public class ColorFadeAnimation extends EffectAnimation{
	
	private static Paint mForeGroundPaint;  
    private static Paint mBackGroundPaint;   
       
    private static int foregrndalpha =255;  
    private static int mAlpha = 0;    
    private boolean toColor;
   
    private HashMap<Integer, Fader> fadeStrategies;
    private Fader fader;
    
    private static Bitmap fadeScreen;  
    
    private ColorMatrix cm;
   
    public ColorFadeAnimation(int startTime, int duration, Fader fader, boolean toColor) 
    {  
    	super(duration, startTime);   	
    	mForeGroundPaint = new Paint();  
    	mBackGroundPaint = new Paint();    	
    	cm = new ColorMatrix(); 
    	this.toColor=toColor;
     
    
    fadeScreen=FadeStrategyProvider.getInstance().getFadeBitmap();
     
     this.fader=fader;
      
     }  
      
    
	@Override
	public void animate() {		    	
    	
     if (toColor)//if the screen is being faded TO color
     {
    	
		if(foregrndalpha > 0)
		{   
				
    			foregrndalpha -= 1;  
    			mAlpha+=2;    			
          
    			fader.fade(cm, mAlpha);
    			
    			mBackGroundPaint.setColorFilter(new ColorMatrixColorFilter(cm));        
    			
    			images.get(0).setFadeTarget(fadeScreen, mBackGroundPaint);
    			
    			    			
        
    		}
     }
     else
     {
    	 	foregrndalpha += 7;  
    	 	mAlpha-=14;
			mForeGroundPaint.setAlpha(foregrndalpha); 
   
			fader.fade(cm, mAlpha);
			
			mBackGroundPaint.setColorFilter(new ColorMatrixColorFilter(cm));        
			
			images.get(0).setFadeTarget(fadeScreen, mBackGroundPaint);
     }
    		
       	
       		
	}
	
	
    

	@Override
	public void adjustParameters() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void undo() {
		
		if (toColor)
		{
			fader.fade(cm, 0);			
			mBackGroundPaint.setColorFilter(new ColorMatrixColorFilter(cm));    
			images.get(0).setFadeTarget(fadeScreen, mBackGroundPaint);
		
    	}
		
		else
		{
			fader.fade(cm, 255);			
			mBackGroundPaint.setColorFilter(new ColorMatrixColorFilter(cm));    
			images.get(0).setFadeTarget(fadeScreen, mBackGroundPaint);
		}
	}
	     

}

